﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Petra.Gis.Geometry.Image.Tiff
{
  //intel = littleEndian, motorola = bigEndian
  internal static class EndianConverter
  {

    internal static TiffDataEndian GetCurrentPlatformEndian()
    {
      if (BitConverter.IsLittleEndian)
        return TiffDataEndian.LittleIndian;
      else
        return TiffDataEndian.BigIndian;

    }




    internal static bool NeedEndianConversion(TiffDataEndian dataEndian)
    {
      if ((dataEndian == TiffDataEndian.BigIndian && BitConverter.IsLittleEndian) || (dataEndian == TiffDataEndian.LittleIndian && !BitConverter.IsLittleEndian))
        return true;
      else
        return false;
    }




    internal static short ToInt16(short val, TiffDataEndian dataEndian)
    {

      //check endian of current platform
      if (NeedEndianConversion(dataEndian))
      {
        byte[] b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return (short)BitConverter.ToInt16(b, 0);
      }
      else
      {
        return val;
      }
    }



    internal static ushort ToUInt16(ushort val, TiffDataEndian dataEndian)
    {

      //check endian of current platform
      if (NeedEndianConversion(dataEndian))
      {
        byte[] b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return BitConverter.ToUInt16(b, 0);
      }
      else
      {
        return val;
      }
    }
    



    internal static int ToInt32(Int32 val, TiffDataEndian dataEndian)
    {

      //check endian of current platform
      if (NeedEndianConversion(dataEndian))
      {
        byte[] b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return BitConverter.ToInt32(b, 0);
      }
      else
      {
        return val;
      }
    }



    internal static uint ToUInt32(UInt32 val, TiffDataEndian dataEndian)
    {

      //check endian of current platform
      if (NeedEndianConversion(dataEndian))
      {
        byte[] b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return BitConverter.ToUInt32(b, 0);
      }
      else
      {
        return val;
      }
    }





    internal static double ToDouble(Double val, TiffDataEndian dataEndian)
    {

      //check endian of current platform
      byte[] b;
      if (NeedEndianConversion(dataEndian))
      {
        b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return BitConverter.ToDouble(b, 0);
      }
      else
      {
        return val;
      }
    }




    internal static float ToFloat(float val, TiffDataEndian dataEndian)
    {

      //check endian of current platform
      byte[] b;
      if (NeedEndianConversion(dataEndian))
      {
        b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return (float)BitConverter.ToSingle(b, 0);

      }
      else
      {
        return val;
      }
    }


    /*
    internal static byte[] ToArray(Double val, WKB.ByteOrder dataEndian)
    {

      //check endian of current platform
      byte[] b;
      if ((dataEndian == WKB.ByteOrder.BigIndian && BitConverter.IsLittleEndian) || (dataEndian == WKB.ByteOrder.LittleIndian && !BitConverter.IsLittleEndian))
      {
        b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return b;
      }
      else
      {
        return BitConverter.GetBytes(val);
      }
    }




    internal static byte[] ToArray(Int32 val, WKB.ByteOrder dataEndian)
    {

      //check endian of current platform
      byte[] b;
      if ((dataEndian == WKB.ByteOrder.BigIndian && BitConverter.IsLittleEndian) || (dataEndian == WKB.ByteOrder.LittleIndian && !BitConverter.IsLittleEndian))
      {
        b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return b;
      }
      else
      {
        return BitConverter.GetBytes(val);
      }
    }
     */
  }
}
